from flask import Blueprint, Flask
from flask_jwt_extended import JWTManager
from datetime import timedelta
from flaskapp.api.v1 import UserAPI, HouseAPI, ChatAPI, AIChatAPI, CollectAPI,AppointmentAPI,ConsultAPI,LandlordAPI, OAuthAPI, AdminAPI, RentAPI, LandlordAPI, ConsultAPI
#from flaskapp.api.v1 import UserAPI, HouseAPI, ChatAPI, AIChatAPI, LandlordAPI, OAuthAPI, AdminAPI, RentAPI #新增房东相关
from flaskapp.config import DBconfig
from flaskapp.util.Sqlalchemy import db
from flask_cors import CORS

def create_app():
    app = Flask(__name__, static_folder='../static')
    # 修改CORS配置，允许所有路由的跨域请求
    CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)

    # JWT配置
    app.config['JWT_SECRET_KEY'] = 'dyt'  # 建议使用环境变量存储
    app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=24)
    jwt = JWTManager(app)

    register_blueprint(app)
    # 导入数据库配置
    app.config.from_object(DBconfig)
    # 创建数据库 sqlalchemy 工具对象
    db.init_app(app)
    return app

def register_blueprint(app):
    bp_v1 = create_blueprint()
    app.register_blueprint(bp_v1, url_prefix="/api")  # url_prefix是给注册的蓝图添加路由前缀。

def create_blueprint():
    v1 = Blueprint("api", __name__)
    UserAPI.api.register(v1)
    HouseAPI.api.register(v1)
    ChatAPI.api.register(v1)
    AIChatAPI.api.register(v1)
    CollectAPI.api.register(v1)
    AppointmentAPI.api.register(v1)
    ConsultAPI.api.register(v1)
    LandlordAPI.api.register(v1)  # 新增注册新的LandlordAPI
    OAuthAPI.api.register(v1)
    AdminAPI.api.register(v1)  
    RentAPI.api.register(v1)  
    ConsultAPI.api.register(v1)  
    return v1